package com.cyf.mq.producer;

import com.cyf.mq.constant.MqConstant;
import com.cyf.mq.pojo.HeroDTO;
import com.cyf.mq.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import java.time.LocalDateTime;

/**
 * @author chenyifan
 * @create 2025-02-18 15:55
 */
@Slf4j
public class DelayProducer {

    public static void main(String[] args) throws Exception{
        DefaultMQProducer producer = new DefaultMQProducer(MqConstant.MESSAGE_PRODUCER_GROUP_A);
        producer.setNamesrvAddr(MqConstant.NAME_SERVER_ADDR);
        producer.start();

        HeroDTO heroDTO = new HeroDTO("jialuo", "伽罗");
        Message message = new Message(MqConstant.MESSAGE_TOPIC_King, JacksonUtil.toJsonString(heroDTO).getBytes());
        // 设置延迟时间
        // messageDelayLevel = "1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h"
        message.setDelayTimeLevel(3);

        SendResult sendResult = producer.send(message);
        log.info("发送成功：{}, 时间：{}", sendResult, LocalDateTime.now());
        producer.shutdown();
    }
}
